﻿#include "Checkpoint.h"
#include"Fighting.h"
#include"GameOver.h"
#include"GameStart.h"
#include"Sprite.h"
#include"Game.h"
#include"GameButton.h"
#include<qlabel.h>
#include<qmovie.h>
#include"GameLevelOne.h"
#include"GameLevelTwo.h"
#include"GameLevelThree.h"
#include"GameLevelFour.h"
#include"GameLevelFive.h"
#include"GameLevelSix.h"
#include"GameLevelSeven.h"
#include"GameLevelEight.h"
Checkpoint::Checkpoint(QSize size, QObject* parent) :
	m_Size(size),QObject(parent)
{
	this->m_parent = parent;
	init(); 
	connect(this, &Checkpoint::GameOverScore, (Game*)parent, &Game::CreateGameOver);
}

Checkpoint::~Checkpoint()
{
	if (m_Fighting != nullptr)
	{
		delete m_Fighting;
		m_Fighting = nullptr;
	}
	
	DeleteButtom();
}

void Checkpoint::init()
{
	NewButtom();
	/*if (m_Fighting == nullptr)
		m_Fighting = new Fighting(m_Size,parent());*/
	setType(Type::checkpoint);
	
}

void Checkpoint::draw(QPainter* painter)
{
	if (m_Fighting != nullptr)
	{
		m_Fighting->draw(painter);
	}
	else
	{
		//绘制背景
		auto temp = Game::res().getPixmap("关卡背景").scaled(m_Size);
		painter->drawPixmap(0, 0, temp);
	}
}
void Checkpoint::NewButtom()
{
	int w = 0, h = 0;
	for (size_t i = 0; i < 8; i++)
	{
		m_LevelButtom.append(new GameButton("关卡按钮", (QWidget*)m_parent));
		auto temp = m_LevelButtom.back();
		w = 0 + i % 2;
		h = i / 2;
		//qDebug() << w<<i;
		temp->move(50 + w * 220, 50 + 100 * h);
		temp->setText(QString::number(i + 1));
		temp->show();
		connectLevel(temp, i);
	}
	if (m_GetBack == nullptr)
	{
		m_GetBack = new GameButton("菜单按钮", (QWidget*)m_parent);
		m_GetBack->move(90, 580);
		m_GetBack->setText("返回");
		m_GetBack->show();
		connect(m_GetBack,&QPushButton::clicked, (Game*)m_parent,&Game::CreateGameStart);
	}
	if (m_Quit == nullptr)
	{
		m_Quit = new GameButton("菜单按钮", (QWidget*)m_parent);
		m_Quit->move(90, 680);
		m_Quit->setText("退出游戏");
		m_Quit->show();
		QObject::connect(m_Quit, &QPushButton::clicked, [=]() {((QWidget*)m_parent)->close(); });
	}

}
void Checkpoint::DeleteButtom()
{
	for (auto& v : m_LevelButtom)
	{
		delete v;
	}
	m_LevelButtom.clear();
	if (m_GetBack != nullptr)
	{
		delete m_GetBack;
		m_GetBack = nullptr;
	}
	if (m_Quit != nullptr)
	{
		delete m_Quit;
		m_Quit = nullptr;
	}
}
void Checkpoint::connectLevel(GameButton* temp, int n)
{
	//decltype(&Checkpoint::LevelOne)p ;
	void (Checkpoint::*p)();
	switch (n)
	{
	case 0:
		p = &Checkpoint::LevelOne; break;
	case 1:
		p = &Checkpoint::LevelTwo; break;
	case 2:
		p = &Checkpoint::LevelThree; break;
	case 3:
		p = &Checkpoint::LevelFour; break;
	case 4:
		p = &Checkpoint::LevelFive; break;
	case 5:
		p = &Checkpoint::LevelSix; break;
	case 6:
		p = &Checkpoint::LevelSeven; break;
	case 7:
		p = &Checkpoint::LevelEight; break;
	default:
		break;
	}
	connect(temp, &QPushButton::clicked, this,p);
}
void Checkpoint::LevelOne()
{
	qDebug() << "关卡1";
	if (m_Fighting == nullptr)
		m_Fighting = new GameLevelOne(m_Size,parent());
	DeleteButtom();
}

void Checkpoint::LevelTwo()
{
	qDebug() << "关卡2";
	if (m_Fighting == nullptr)
		m_Fighting = new GameLevelTwo(m_Size, parent());
	DeleteButtom();
}
void Checkpoint::LevelThree()
{
	qDebug() << "关卡3";
	if (m_Fighting == nullptr)
		m_Fighting = new GameLevelThree(m_Size, parent());
	DeleteButtom();
}
void Checkpoint::LevelFour()
{
	qDebug() << "关卡4";
	if (m_Fighting == nullptr)
		m_Fighting = new GameLevelFour(m_Size, parent());
	DeleteButtom();
}
void Checkpoint::LevelFive()
{
	qDebug() << "关卡5";
	if (m_Fighting == nullptr)
		m_Fighting = new GameLevelFive(m_Size, parent());
	DeleteButtom();
}
void Checkpoint::LevelSix()
{
	qDebug() << "关卡6";
	if (m_Fighting == nullptr)
		m_Fighting = new GameLevelSix(m_Size, parent());
	DeleteButtom();
}
void Checkpoint::LevelSeven()
{
	qDebug() << "关卡7";
	if (m_Fighting == nullptr)
		m_Fighting = new GameLevelSeven(m_Size, parent());
	DeleteButtom();
}
void Checkpoint::LevelEight()
{
	qDebug() << "关卡8";
	if (m_Fighting == nullptr)
		m_Fighting = new GameLevelEight(m_Size, parent());
	DeleteButtom();
}

void Checkpoint::update()
{
	if (m_Fighting != nullptr)
		updateFighting();
}
void Checkpoint::updateFighting()
{
	m_Fighting->update();
	if (!m_Fighting->getPtrPlayer()->isActive())//玩家已死，结束战斗模块
	{
		GameOverScore(m_Fighting->getPtrPlayer()->getTotalScore());
		//qDebug() << "游戏结束";
	}
}